package edu.hawaii.myisern.action;

import java.util.ArrayList;
import java.util.List;
import edu.hawaii.myisern.model.Log;
import edu.hawaii.myisern.model.Model;
import edu.hawaii.myisern.model.MyOrganization;
import net.sourceforge.stripes.action.DontValidate;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.validation.Validate;

/**
 * New Organization Action Bean.
 * 
 * @author rcox
 * 
 */
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
public class EditOrganizationActionBean extends MyIsernActionBean {

  @Validate(required = true)
  private String name;
  private String contact;
  private String homePage;
  private String country;
  private List<String> affiliatedResearchers = new ArrayList<String>();
  private String researchDescription;
  private List<String> researchKeywords = new ArrayList<String>();
  private String type;
  private String delAffiliatedResearcher;
  private String newAffiliatedResearcher;
  private String newResearchKeyword;
  private String importFile;
  private String exportFile;

  /**
   * Get researcher to delete.
   * 
   * @return the delAffiliatedResearcher
   */
  public String getDelAffiliatedResearcher() {
    return delAffiliatedResearcher;
  }

  /**
   * Set researcher to delete.
   * 
   * @param delAffiliatedResearcher the delAffiliatedResearcher to set
   */
  public void setDelAffiliatedResearcher(String delAffiliatedResearcher) {
    this.delAffiliatedResearcher = delAffiliatedResearcher;
  }

  /**
   * Get affiliated researchers string.
   * 
   * @return the affiliatedResearchersStr
   */
  public String getNewAffiliatedResearcher() {
    return newAffiliatedResearcher;
  }

  /**
   * Set affiliated researchers string.
   * 
   * @param newAffiliatedResearcher the affiliatedResearchersStr to set
   */
  public void setNewAffiliatedResearcher(String newAffiliatedResearcher) {
    this.newAffiliatedResearcher = newAffiliatedResearcher;
  }

  /**
   * Get researcher key words string.
   * 
   * @return the researchKeywordsStr
   */
  public String getNewResearchKeyword() {
    return newResearchKeyword;
  }

  /**
   * Set research key words string.
   * 
   * @param researchKeywordsStr the researchKeywordsStr to set
   */
  public void setNewResearchKeyword(String researchKeywordsStr) {
    this.newResearchKeyword = researchKeywordsStr;
  }

  /**
   * Gets the contact.
   * 
   * @return the contact
   */
  public String getContact() {
    return contact;
  }

  /**
   * Sets the contact.
   * 
   * @param contact the contact to set
   */
  public void setContact(String contact) {
    this.contact = contact;
  }

  /**
   * Gets the name.
   * 
   * @return the name
   */
  public String getName() {
    return name;
  }

  /**
   * Sets the name.
   * 
   * @param name the name to set
   */
  public void setName(String name) {
    this.name = name;
  }

  /**
   * Get the home page.
   * 
   * @return the homePage
   */
  public String getHomePage() {
    return homePage;
  }

  /**
   * Sets the home page.
   * 
   * @param homePage the homePage to set
   */
  public void setHomePage(String homePage) {
    this.homePage = homePage;
  }

  /**
   * Gets the country.
   * 
   * @return the country
   */
  public String getCountry() {
    return country;
  }

  /**
   * Sets the country.
   * 
   * @param country the country to set
   */
  public void setCountry(String country) {
    this.country = country;
  }

  /**
   * Gets the affiliated researchers.
   * 
   * @return the affiliatedResearchers
   */
  public List<String> getAffiliatedResearchers() {
    Log.enter("EditOrganizationActionBean.getAffiliatedReseacher start.");
    for (String item : this.affiliatedResearchers) {
      Log.enter("AffilicatedResearchers =" + item);
    }
    List<String> list = new ArrayList<String>();
    list.addAll(this.affiliatedResearchers);
    Log.enter("EditOrganizationActionBean.getAffiliatedReseacher end.");
    return list;
  }

  /**
   * Sets the affiliated researchers.
   * 
   * @param affiliatedResearchers the affiliatedResearchers to set
   */
  public void setAffiliatedResearchers(List<String> affiliatedResearchers) {
    Log.enter("EditOrganizationActionBean.setAffiliatedReseacher start.");
    List<String> list = new ArrayList<String>();
    list.addAll(affiliatedResearchers);
    this.affiliatedResearchers = list;
    for (String item : this.affiliatedResearchers) {
      Log.enter("AffilicatedResearchers =" + item);
    }
    Log.enter("EditOrganizationActionBean.setAffiliatedReseacher end.");
  }

  /**
   * Get the research description.
   * 
   * @return the researchDescription
   */
  public String getResearchDescription() {
    return researchDescription;
  }

  /**
   * Set the research description.
   * 
   * @param researchDescription the researchDescription to set
   */
  public void setResearchDescription(String researchDescription) {
    this.researchDescription = researchDescription;
  }

  /**
   * Get the research keywords.
   * 
   * @return the researchKeywords
   */
  public List<String> getResearchKeywords() {
    return researchKeywords;
  }

  /**
   * Set the research keywords.
   * 
   * @param researchKeywords the researchKeywords to set
   */
  public void setResearchKeywords(List<String> researchKeywords) {
    this.researchKeywords = researchKeywords;
  }

  /**
   * Get type.
   * 
   * @return the type
   */
  public String getType() {
    return type;
  }

  /**
   * Set type.
   * 
   * @param type the type to set
   */
  public void setType(String type) {
    this.type = type;
  }

  /**
   * @return the importFile
   */
  public String getImportFile() {
    return importFile;
  }

  /**
   * @param importFile the importFile to set
   */
  public void setImportFile(String importFile) {
    this.importFile = importFile;
  }

  /**
   * @return the exportFile
   */
  public String getExportFile() {
    return exportFile;
  }

  /**
   * @param exportFile the exportFile to set
   */
  public void setExportFile(String exportFile) {
    this.exportFile = exportFile;
  }

  /**
   * Loads a record on to the form ready for editing.
   * 
   * @return resolution of next page.
   */
  @DontValidate
  public Resolution preEdit() {
    MyOrganization organization = new MyOrganization();
    if (this.name != null) {
      organization = this.model.getOrganization(this.name);
    }
    if (organization == null) {
      organization = new MyOrganization();
      organization.setName(this.name);
    }
    this.setContact(organization.getContact());
    this.setType(organization.getType());
    this.setCountry(organization.getCountry());
    this.setAffiliatedResearchers(organization.getAffiliatedResearchers());
    this.setResearchDescription(organization.getResearchDescription());
    this.setResearchKeywords(organization.getResearchKeywords());
    this.setHomePage(organization.getHomePage());
    return new RedirectResolution("/editOrganization.jsp").flash(this);
  }

  /**
   * Delete record.
   * 
   * @return resolution back to same page.
   */
  public Resolution delete() {
    this.model.deleteOrganization(this.name);
    return new RedirectResolution("/selectOrganization.jsp").flash(this);
  }

  /**
   * Add researcher to affiliated researcher list.
   * 
   * @return resolution to main menu.
   */
  @DontValidate
  public Resolution addResearcher() {
    List<String> list = this.getAffiliatedResearchers();
    if ((this.newAffiliatedResearcher != null) && (!this.newAffiliatedResearcher.isEmpty())) {
      Boolean found = false;
      for (String item : this.affiliatedResearchers) {
        if (this.newAffiliatedResearcher.equalsIgnoreCase(item)) {
          found = true;
        }
      }
      if (!found) {
        list.add(this.newAffiliatedResearcher);
      }
    }
    this.setAffiliatedResearchers(list);
    this.newAffiliatedResearcher = "";
    return new RedirectResolution("/editOrganization.jsp").flash(this);
  }

  /**
   * Add researcher to affiliated researcher list.
   * 
   * @return resolution to main menu.
   */
  @DontValidate
  public Resolution addKeyword() {
    if ((this.newResearchKeyword != null) && (!this.newResearchKeyword.isEmpty())) {
      Boolean found = false;
      for (String item : this.researchKeywords) {
        if (this.newResearchKeyword.equalsIgnoreCase(item)) {
          found = true;
        }
      }
      if (!found) {
        this.researchKeywords.add(this.newResearchKeyword);
      }
    }
    this.newResearchKeyword = "";
    return new RedirectResolution("/editOrganization.jsp").flash(this);
  }

  /**
   * Save the record.
   * 
   * @return resolution to main menu.
   */
  public Resolution save() {
    MyOrganization organization = new MyOrganization();
    organization.setName(this.getName());
    organization.setContact(this.getContact());
    organization.setType(this.getType());
    organization.setCountry(this.getCountry());
    organization.setAffiliatedResearchers(this.getAffiliatedResearchers());
    organization.setResearchDescription(this.getResearchDescription());
    organization.setResearchKeywords(this.getResearchKeywords());
    organization.setHomePage(this.getHomePage());
    this.model.saveOrganization(organization);
    return new RedirectResolution("/selectOrganization.jsp");
  }

  /**
   * Cancel and return to main menu.
   * 
   * @return resolution to main menu.
   */
  @DontValidate
  public Resolution cancel() {
    return new RedirectResolution("/selectOrganization.jsp");
  }

  /**
   * Clear data.
   * 
   * @return resolution back to same page.
   */
  @DontValidate
  public Resolution clearData() {
    Model.getInstance().clearOrganizations();
    return new RedirectResolution("/selectOrganization.jsp").flash(this);
  }

  /**
   * Imports xml data.
   * 
   * @return resolution back to same page.
   */
  @DontValidate
  public Resolution importData() {
    Model.getInstance().importOrganizations(this.importFile);
    return new RedirectResolution("/selectOrganization.jsp").flash(this);
  }

  /**
   * Exports xml data.
   * 
   * @return resolution back to same page.
   */
  @DontValidate
  public Resolution exportData() {
    Model.getInstance().exportOrganizations(this.exportFile);
    return new RedirectResolution("/selectOrganization.jsp").flash(this);
  }

}
